package com.davinci.freemarket.businessLayer.servlets;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.davinci.freemarket.businessLayer.controllers.PublicationsController;
import com.davinci.freemarket.businessLayer.utils.WebParamsStandarized;
import com.davinci.freemarket.dataAccessLayer.accessdata.user.UserDAO;
import com.davinci.freemarket.dataModelLayer.model.DBAttibuteModel;
import com.davinci.freemarket.dataModelLayer.model.PublicationModel;
import com.davinci.freemarket.dataModelLayer.model.UserModel;

/**
 * Servlet implementation class GetPublicationServlet
 */
@WebServlet("/GetPublicationServlet")
public class GetPublicationServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetPublicationServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		//-- almaceno el valor del parametro
		String id_publicacion = request.getParameter(WebParamsStandarized.PARAMETER_PUBLICATION_ID);
		
		if(id_publicacion == null || id_publicacion == "")
			return;
		
		//-- genero los parametros 	<NOMBRE_COLUMNA>, <VALOR>
		ArrayList<DBAttibuteModel> conditions = new ArrayList<DBAttibuteModel>();
		conditions.add(new DBAttibuteModel("ID_PUBLI", id_publicacion));

		//-- inicio instancia de controller
		PublicationsController pcontroller = new PublicationsController();
		
		try {
			//-- pido la puclicacion con el ID provisto
			ArrayList<PublicationModel> publication = pcontroller.getPublication(conditions);
			
			HttpSession session = request.getSession(true);
			session.setAttribute("publicationByID", publication);
			
			RequestDispatcher dispatcher = request.getRequestDispatcher("../publicationDetails.jsp");
			dispatcher.forward(request, response);
		} 
		catch (Throwable theException) {
			System.out.println(theException); 
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
